package com.by.yz.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;

import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 123 on 2016/10/8.
 */
public class EditCarActivity extends BaseActivity {

    private Spinner mySpinner;
    private List<String> datas = new ArrayList<String>();
    private ArrayAdapter<String> adapter;

    private TextView city;
    private TextView firstTravel;
    private TextView currentTravel;
    private TextView insuranceEnd;
    private Button next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_car_activity);
        initDatas();
        initView();

    }

    private void initDatas() {
        datas.add("桂");
        datas.add("粤");
        datas.add("川");
        datas.add("京");
        datas.add("贵");
    }

    private void initView() {

        setToolbarTitle("编辑车辆");
        city = (TextView) findViewById(R.id.Spinner_city_text);

        firstTravel = (TextView) findViewById(R.id.first_travel);
        firstTravel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(EditCarActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        firstTravel.setText(String.format("%d-%d-%d",year,monthOfYear+1,dayOfMonth));
                    }
                },2000,1,2).show();
            }
        });

        currentTravel = (TextView) findViewById(R.id.current_travel);
        currentTravel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(EditCarActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        currentTravel.setText(String.format("%d-%d-%d",year,monthOfYear+1,dayOfMonth));
                    }
                },2000,1,2).show();
            }
        });

        insuranceEnd = (TextView) findViewById(R.id.insurance_end);
        insuranceEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(EditCarActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        insuranceEnd.setText(String.format("%d-%d-%d",year,monthOfYear+1,dayOfMonth));
                    }
                },2000,1,2).show();
            }
        });

        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EditCarActivity.this, ManageCar2Activity.class);
                EditCarActivity.this.startActivity(intent);
                EditCarActivity.this.overridePendingTransition(R.anim.push_left_in,
                        R.anim.push_left_out);
            }
        });


        mySpinner = (Spinner) findViewById(R.id.Spinner_city);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, datas);
        //第三步：为适配器设置下拉列表下拉时的菜单样式。
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //第四步：将适配器添加到下拉列表上
        mySpinner.setAdapter(adapter);
        //第五步：为下拉列表设置各种事件的响应，这个事响应菜单被选中
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                /* 将所选mySpinner 的值带入myTextView 中*/
                city.setText(adapter.getItem(arg2));
                /* 将mySpinner 显示*/
                arg0.setVisibility(View.VISIBLE);
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                //   myTextView.setText("NONE");
                arg0.setVisibility(View.VISIBLE);
            }
        });
        /*下拉菜单弹出的内容选项触屏事件处理*/
        mySpinner.setOnTouchListener(new Spinner.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                /**
                 *
                 */
                return false;
            }
        });
        /*下拉菜单弹出的内容选项焦点改变事件处理*/
        mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub

            }
        });


    }


//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true;
//        getMenuInflater().inflate(R.menu.mm, menu);
//        return true;
//    }

//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()) {
//           case android.R.id.home:
//                Toast.makeText(this, "home", Toast.LENGTH_SHORT).show();
//                return true;
//            case R.id.m1:
//                Toast.makeText(this, "m1", Toast.LENGTH_SHORT).show();
//                break;
//            case R.id.m2:
//                Toast.makeText(this, "m2", Toast.LENGTH_SHORT).show();
//                break;
//
//        }
//        return super.onOptionsItemSelected(item);
//    }
}
